package com.jshoperxms.controller.mall.backstage.setting;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.jshoperxms.Response;
import com.jshoperxms.components.redis.RedisUtils;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.utils.json.GsonJson;
import com.jshoperxms.controller.utils.statickey.RedisKey;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.entity.GlobalParamM;
import com.jshoperxms.service.GlobalParamService;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by sdywcd on 17/3/9.
 * Des:
 */
@Controller
@RequestMapping("/mall/setting")
public class SysParamsController extends BaseTController{

    @Resource
    private RedisUtils redisUtils;


    @Resource
    private GlobalParamService globalParamService;

    private Map<String,Object>responsedata=new HashMap<>();


    @RequestMapping(value="/find",method= RequestMethod.POST)
    public ResponseEntity find() {
        String configstrs=this.redisUtils.getObjectByKey(RedisKey.CONFIG);
        Gson gson = new GsonBuilder().enableComplexMapKeySerialization()
                .create();
        Map<String, String> configs = gson.fromJson(
                configstrs,
                new TypeToken<Map<String, String>>() {
                }.getType());
        if(configs!=null){
            responsedata.put("configs",configs);
            responsedata.put(TagWords.SUCCESS, true);
            responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        }
        return Response.success(responsedata);
    }


    @RequestMapping(value="/update",method= RequestMethod.POST)
    public ResponseEntity update(@RequestBody SysParamVo rq){
        Gson gson=new GsonBuilder().enableComplexMapKeySerialization().create();
        Map<String,String>configs=new LinkedHashMap<String, String>();
        configs.put(SysGlobalParam.ICP, rq.getIcp());
        configs.put(SysGlobalParam.ISSAVEIMGTOCLOUD,rq.getIssaveimgtocloud());
        configs.put(SysGlobalParam.ISSAVEORIGINALBITMAP,rq.getIssaveoriginalbitmap());
        configs.put(SysGlobalParam.ISIMAGECOMPRESSION, rq.getIsimagecompression());
        configs.put(SysGlobalParam.DEFAULTHEIGHT, rq.getDefaultheight());
        configs.put(SysGlobalParam.DEFAULTWIDTH, rq.getDefaultwidth());
        configs.put(SysGlobalParam.THUMBNAILHEIGHTLOW, rq.getThumbnailheightlow());
        configs.put(SysGlobalParam.THUMBNAILWIDTHLOW, rq.getThumbnailwidthlow());
        configs.put(SysGlobalParam.THUMBNAILHEIGHTMIDDLE, rq.getThumbnailheightmiddle());
        configs.put(SysGlobalParam.THUMBNAILWIDTHMIDDLE, rq.getThumbnailwidthmiddle());
        configs.put(SysGlobalParam.THUMBNAILHEIGHTHIGH, rq.getThumbnailheighthigh());
        configs.put(SysGlobalParam.THUMBNAILWIDTHHIGH, rq.getThumbnailwidthhigh());
        configs.put(SysGlobalParam.FREEFREIGHT, rq.getFreefreight());
        configs.put(SysGlobalParam.ISUSEPOINTTODISCOUNTORDERAMOUNT, rq.getIsusepointtodiscountorderamount());
        configs.put(SysGlobalParam.INTEGRALCONVERSIONRATION, rq.getIntegralconversionration());
        configs.put(SysGlobalParam.ISUSEVOUCHERDISCOUNTORDERAMOUNT, rq.getIsusevoucherdiscountorderamount());
        configs.put(SysGlobalParam.FREIGHT, rq.getFreight());
        Criterion criterion=Restrictions.eq("gkey", RedisKey.CONFIG);
        GlobalParamM globalParamM=this.globalParamService.findOneByCriteria(GlobalParamM.class, criterion);
        if(globalParamM!=null){
            globalParamM.setGvalue(gson.toJson(configs));
            this.globalParamService.update(globalParamM);
            this.redisUtils.setObject(RedisKey.CONFIG, GsonJson.parseDataToJson(configs));
            responsedata.put("configs",configs);
            responsedata.put(TagWords.SUCCESS, true);
            responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        }
        return Response.success(responsedata);
    }

}
